package cn.legend.travel.tour.team.dao.search.impl;

import cn.legend.travel.common.enumerator.ServiceCode;
import cn.legend.travel.common.exception.ServiceException;
import cn.legend.travel.common.pojo.vo.PageData;
import cn.legend.travel.tour.team.dao.search.ITourSearchRepository;
import cn.legend.travel.tour.team.dao.search.TourElasticSearchRepository;
import cn.legend.travel.tour.team.pojo.po.TourSearchPO;
import cn.legend.travel.tour.team.pojo.vo.TourSearchVO;
import cn.legend.travel.tour.team.utils.TourUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.SearchPage;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class TourSearchRepositoryImpl implements ITourSearchRepository {
    @Autowired
    TourElasticSearchRepository tourElasticSearchRepository;
    @Autowired
    TourElasticsearchRepositoryImpl elasticsearchRepository;
    @Value("${legend.dao.default-query-page-size}")
    private Integer pageSize;


    @Override
    public PageData<TourSearchVO> search(String keyword,String departure, Integer pageNum) {
        Pageable pageable = PageRequest.of(pageNum-1,pageSize);
        SearchPage<TourSearchPO> searchPage = elasticsearchRepository.customSearch(keyword,departure, pageable);

        return convertSearchPageToPageData(searchPage);
    }

    @Override
    public PageData<TourSearchVO> searchByType(String type, Integer pageNum) {
        Pageable pageable = PageRequest.of(pageNum-1,pageSize);
        SearchPage<TourSearchPO> searchPage = elasticsearchRepository.searchByType(type, pageable);
        return convertSearchPageToPageData(searchPage);
    }
    private PageData<TourSearchVO> convertSearchPageToPageData(SearchPage<TourSearchPO> searchPage) {
        SearchHits<TourSearchPO> searchHits = searchPage.getSearchHits();
        List<TourSearchVO> tourSearchList = new ArrayList<>();
        for (SearchHit<TourSearchPO> searchHit : searchHits) {
            tourSearchList.add(convertSearchHitToVO(searchHit));
        }
        PageData<TourSearchVO> pageData = new PageData<>();
        pageData.setCurrentPage(searchPage.getNumber());
        pageData.setPageSize(searchPage.getSize());
        pageData.setMaxPage(searchPage.getTotalPages());
        pageData.setTotal(searchHits.getTotalHits());

        pageData.setList(tourSearchList);
        return pageData;
    }

    private TourSearchVO convertSearchHitToVO(SearchHit<TourSearchPO> searchHit) {
        TourSearchPO tourSearchPO = searchHit.getContent();
        List<String> highlightFields = searchHit.getHighlightField("title");
        if(tourSearchPO == null){
            String message = "没有搜索的商品";
            throw new ServiceException(ServiceCode.ERROR_BAD_REQUEST,message);
        }
        // 将po转化为vo
        TourSearchVO tourSearchVO = TourUtils.copySearchPO2VO(tourSearchPO);
        if (highlightFields.size() > 0) {
            tourSearchVO.setTitle(highlightFields.get(0));
        }
        return tourSearchVO;
    }
}
